package com.aliang.shardingjdbcdemo.conf;


import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.KeyGeneratorConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Author: chendongliang
 * @Date: 2019/8/23 14:32
 *
 *
 * 分库分表数据源配置
 *
 *
 */
//@Configuration
public class DataSourceConfig {



    @Bean("dataSource")
    public DataSource getDataSource() throws SQLException {
        return buildDataSource();
    }

    private DataSource buildDataSource() throws SQLException {
        /*** 1.设置数据源MAP 映射，***/
        Map<String,DataSource> dataSourceMap =  new HashMap<>();
        dataSourceMap.put("ds_0",createDataSource("ds_0"));
        dataSourceMap.put("ds_1",createDataSource("ds_1"));

        /*** sharding 规则配置 ***/
        ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration();
        /*** 设置表路由配置 ***/
        shardingRuleConfiguration.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
        /*** 绑定表，逻辑表名 ***/
        shardingRuleConfiguration.setBindingTableGroups(Arrays.asList("t_order"));
        /*** 设置数据库路由规则 ***/
        shardingRuleConfiguration.setDefaultDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id","ds_${user_id % 2}"));
        /*** 设置表路由规则 ***/
        shardingRuleConfiguration.setDefaultTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id","t_order_${order_id % 2}"));

        /*** 其他配置，sql.show 打印执行sql语句 ***/
        Properties properties = new Properties();
        properties.put("sql.show",true);
        /*** 构建数据源 ***/
        DataSource dataSource = ShardingDataSourceFactory.createDataSource(dataSourceMap,shardingRuleConfiguration, properties);

        return dataSource;
    }


    public static KeyGeneratorConfiguration getKeyGeneratorConfiguration(){
        return new KeyGeneratorConfiguration("SNOWFLAKE","user_id");
    }

    /*** 逻辑表对应物理表规则配置 ***/
    TableRuleConfiguration getOrderTableRuleConfiguration(){
        TableRuleConfiguration tableRuleConfiguration =new TableRuleConfiguration("t_order","ds_${0..1}.t_order_${0..1}");
        tableRuleConfiguration.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
        return tableRuleConfiguration;
    }


    /***
     * 创建数据源
     *
     * ***/
    public static DataSource createDataSource(String dbName){

        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        druidDataSource.setUrl(String.format("jdbc:mysql://116.62.18.242:3306/%s?characterEncoding=utf-8", dbName));

        return druidDataSource;
    }

}
